package hng.service.impl;

import hng.dto.ClientProductDTO;
import hng.mapper.BannerMapper;
import hng.mapper.NavMapper;
import hng.mapper.ProductMapper;
import hng.model.BannerDO;
import hng.model.NavDO;
import hng.service.ClientService;
import hng.service.admin.NavService;
import hng.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 告白
 * @since 2024-06-27
 */
@Service
public class ClientServiceImpl implements ClientService {

    @Autowired(required = false)
    private NavMapper navMapper;

    @Autowired(required = false)
    private BannerMapper bannerMapper;

    @Autowired(required = false)
    private ProductMapper productMapper;

    /**
     * 导航栏列表
     */
    @Override
    public List<NavDO> navList() {
        List<NavDO> navDOList = navMapper.selectList(null);
        return navDOList;
    }

    /**
     * 轮播图列表
     * @return
     */
    @Override
    public List<BannerDO> bannerList() {
        List<BannerDO> bannerDOList = bannerMapper.selectList(null);
        return bannerDOList;
    }

    /**
     * 获取首页分类下的商品
     */
    @Override
    public List<ClientProductDTO> productList() {
        String[] name = {"水果", "蔬菜", "水产"};
        List<ClientProductDTO> list = new ArrayList<>(3);
        List<ProductVO> productVOList = productMapper.clientProduct();

        for (int i = 0; i <= 2; i++) {
            Integer idx = i + 1;
            List<ProductVO> items = productVOList.stream().filter(obj -> idx == obj.getType()).collect(Collectors.toList());
            ClientProductDTO clientProductDTO = new ClientProductDTO();
            clientProductDTO.setType(i + 1);
            clientProductDTO.setName(name[i]);
            clientProductDTO.setProductList(items);
            list.add(clientProductDTO);
        }

        return list;
    }

}
